package api

import (
	"dev.ixmxm.top/rms/codes"
	"dev.ixmxm.top/rms/utils/json"
	"dev.ixmxm.top/rms/utils/tools"
)

func NewResponse(requestID string, data interface{}, code codes.Code) *response {
	r := &response{
		ResultData:  json.Dump(data),
		ResultID:    requestID,
		ResultStamp: tools.GenMicrosecondTimeStamp(),
	}
	if code == nil {
		r.ResultCode = 0
		r.ResultInfo = "成功"
	} else {
		r.ResultCode = code.GetCode()
		r.ResultInfo = code.Error()
		r.Details = code.GetDetails()
		if len(r.Details) > 0 {
			for _, temp := range r.Details {
				r.ResultInfo += ";" + temp
			}
		}
		if code.Unwrap() != nil {
			r.ResultInfo += ";" + code.Unwrap().Error()
		}
	}
	return r
}

type response struct {
	ResultCode  uint32   `json:"code"`    //结果编号，0=成功
	ResultInfo  string   `json:"message"` //信息
	ResultData  json.Raw `json:"data"`    //数据
	ResultID    string   `json:"id"`      //请求ID
	ResultStamp string   `json:"stamp"`   //时间戳
	Details     []string `json:"details"` //详细说明
}

type ResponseExample struct {
	ResultCode  uint32   `json:"code"`    //结果编号，0=成功
	ResultInfo  string   `json:"message"` //返回信息
	ResultID    string   `json:"id"`      //请求ID
	ResultStamp string   `json:"stamp"`   //时间戳
	Details     []string `json:"details"` //详细说明
}

type ResponseExampleStruct struct {
	ResponseExample
	ResultData ResponseData `json:"data"` //返回数据
}

type ResponseExampleList struct {
	ResponseExample
	ResultData []ResponseData `json:"data"` //返回数据
}

type ResponseData struct {
}
